
rm(list=ls())

files=dir("Data/")

data=NULL

for (i in 1:length(files)) {
  tmp=read.table(paste("Data/",files[i],sep=""),skip=0,header = F)
  data=rbind(data,cbind(i,tmp))
}

colnames(data)=c("sub","trlNum","blkNum","cond","correct","rt","resp")

data[,"cond"][data[,"cond"]==1]="comp"
data[,"cond"][data[,"cond"]==2]="incomp"

data[,"resp"][data[,"resp"]==1]="left"
data[,"resp"][data[,"resp"]==2]="right"


data=data[data[,"rt"]<1.5,]
data=data[data[,"rt"]>0.1,]


parsedData=data

data=list()

subs=unique(parsedData[,"sub"])
S=length(subs)

for (s in 1:S) {
  data[[s]]=list(Time=NULL,Resp=NULL,Cond=NULL,Stim=NULL)
  
  data[[s]]$Time=as.numeric(parsedData[,"rt"][parsedData[,"sub"]==subs[s]])
  data[[s]]$Cond=as.factor(parsedData[,"cond"][parsedData[,"sub"]==subs[s]])
  data[[s]]$Resp=(1-as.numeric(parsedData[,"correct"][parsedData[,"sub"]==subs[s]]))+1
  stim=rep(NA,length(data[[s]]$Resp))
  stim[data[[s]]$Resp==1]=parsedData[,"resp"][parsedData[,"sub"]==subs[s] & parsedData[,"correct"]==1]
  stim[data[[s]]$Resp==2 & parsedData[,"resp"][parsedData[,"sub"]==subs[s]]=="left"]="right"
  stim[data[[s]]$Resp==2 & parsedData[,"resp"][parsedData[,"sub"]==subs[s]]=="right"]="left"
  data[[s]]$Stim=as.factor(stim)
}


conds=c("comp","incomp")
stims=c("left","right")


save(data,conds,stims,S,subs,file="parsedData-servantFlanker.Rdata")

